# coding=utf-8

'''
Created on Mar 23, 2013

@author: Petko
'''
import unittest
from utils.sententialyzer_client import SententialyzerClient
from sententialyzer.settings import SENTENTIALYZER_ADMIN_USER_NAME,\
    SENTENTIALYZER_DEFAULT_GROUP_NAME
import json
from urllib2 import HTTPError


class setup_moderators(unittest.TestCase):
    sc = SententialyzerClient()

    def setUp(self):
        pass


    def tearDown(self):
        pass


    def testSetupModetators(self):
        #self.sc.authorize('testname', 'testpass', 'noscope')
        
        #setup moderators
        self.sc.authorize('mod1', 'nopass', 'bopin_r')
        self.sc.authorize('mod2', 'nopass', 'bopin_r')
        
        self.sc.authorize(SENTENTIALYZER_ADMIN_USER_NAME, 'nopass', 'group_adm view_adm')
        
        group = self.sc.createGroup('article_moderators', 'empty', 'bopin_c bopin_d bopin_r')
       
        print self.sc.addUserToGroup('mod1', 'article_moderators')
        print self.sc.addUserToGroup('mod2', 'article_moderators')
        
        #setup the views
        
        try:
            result = self.sc.postDeleteView("Bulgaria")
        except HTTPError:
            pass
        try:
            result = self.sc.postDeleteView("France")
        except HTTPError:
            pass
        try:
            result = self.sc.postDeleteView("Australia")
        except HTTPError:
            pass
        
        try:
            result = self.sc.postDeleteView("WiredScience")
        except HTTPError:
            pass
        
        print self.sc.postCreateView('Bulgaria', "article_moderators:m sent_default_group:l")
        print self.sc.postCreateView('France', "article_moderators:m sent_default_group:l")
        print self.sc.postCreateView('Australia', "article_moderators:m sent_default_group:l")
        print self.sc.postCreateView('WiredScience', "article_moderators:m sent_default_group:l")
        
        deleteAllOps = True
        if deleteAllOps:
            self.sc.authorize('mod1', 'test', 'bopin_r bopin_d')
            result = self.sc.viewListOp('all_base_opinions', 'newest', access_token = self.sc.accessToken)
            page = result['page']
            for op in page:
                self.sc.deleteBaseOp(op['id'])
                
        testData = True
        if testData:
            #add some test data
            self.sc.authorize("mod1", 'nopass', 'bopin_c')
            accessTokenBackup = self.sc.accessToken
            
            bgOpinions = []
            
            for i in range(1, 5):
                self.sc.accessToken = accessTokenBackup 
                baseOp = self.sc.createBaseOp("BG op " + str(i), "content content content content ", "article_moderators:d sent_default_group:rc")
                
                bgOpinions.append(baseOp['id'])
                
                for j in range(1, i):
                    self.sc.authAndCreateOp(None, baseOp['id'], ["positive", "negative", "neutral"][j % 3], title="some comment")
            
            self.sc.authorize(SENTENTIALYZER_ADMIN_USER_NAME, 'nopass', 'group_adm view_adm')
            result = self.sc.addOpsToView("Bulgaria", bgOpinions)
            
            self.sc.authorize("mod1", 'nopass', 'bopin_c')
            
            baseOp = self.sc.createBaseOp("Some Monkeys Have Conversations That Resemble Ours", '''
The sounds of marmoset monkeys chattering may hint at the mysterious origins of human language.<br>
<br>
A new study shows that marmosets exchange calls in a precisely timed, back-and-forth fashion typical of human conversation, but not found in other primates. The monkeys don't appear to have a language, but the timing suggests the foundations of our own.<br>
<br>
"That could be the foundation of more sophisticated things, like syntax," said psychologist Asif Ghazanfar of Princeton University, co-author of the study, which was published today in Current Biology. "You can't have any of those other really cool aspects of language without first having this."<br>
<br>
How language, so complex and information-rich, evolved in Homo sapiens and, as far as we know, no other species, is one of anthropology's outstanding questions. The traditional, seemingly intuitive answer is that it arose from the vocalizations of ancestors who were capable of a few rudimentary noises and wanted to say more.<br>
(full text at <a href="http://www.wired.com/wiredscience/2013/10/monkey-conversations-language-origins/">http://www.wired.com/wiredscience/2013/10/monkey-conversations-language-origins/<a>)
''' 
, "article_moderators:d sent_default_group:rc")
            
            self.sc.authorize(SENTENTIALYZER_ADMIN_USER_NAME, 'nopass', 'group_adm view_adm')
            result = self.sc.addOpsToView("WiredScience", [baseOp['id']])
            
            self.sc.authorize("Guest", 'nopass', 'all:c')
            op = self.sc.createOp(baseOp['id'], "negative", title='''
why no refernence to previous studies using information theoary showing dolphins have a language? 
"The methods are based on information theory, a mathematical way of analysing any sequence of symbols - a series of DNA bases, numbers, letters, phonemes, words or phrases, for instance - to see whether it contains information. According to the theory, the information content of a message is distinct from its meaning, relating instead to the number of 1's or 0's needed to encode it. The theory also offers a way to gauge the complexity of a given communication system: as complexity increases, more information can be conveyed."
            ''')
            
            self.sc.authAndCreateOp('', op['id'], 'positive')
            self.sc.authAndCreateOp('', op['id'], 'positive')
            self.sc.authAndCreateOp('', op['id'], 'positive')
            
            self.sc.authorize("jerry", 'nopass', 'all:c')
            op = self.sc.createOp(op['id'], "positive", title="the most comprehensive response so far good job Guest")
            
            self.sc.authAndCreateOp('', op['id'], 'positive')
            
            self.sc.authorize("Tadatadum", 'nopass', 'all:c')
            op = self.sc.createOp(baseOp['id'], "positive", title='''
My dog takes turns when barking at and responding to dogs on the other side of the lake. Its usually 1 bark, wait for response, bark twice, wait for response, if none then 3 quick barks, wait for response then two if one comes back. We call it being on the phone. Our other dog, deceased before this one came along did the same thing.
            ''')
            
            for i in range(1,6):
                self.sc.authAndCreateOp('', op['id'], 'positive')
            
            self.sc.authorize("DavidBoulton", 'nopass', 'all:c') 
            op = self.sc.createOp(baseOp['id'], "neutral", title='''
In a conversation with Dr. Terrence Deacon of the "Symbolic Species" (for Children of the Code) We discuss how language is a property of a collective, not an individual. And that human language co-evolved with our brains in the context of further extending our capacity for 'learning together'. Why is human language so different than the communication systems of all other creatures? Because the human capacity for learning, individually and collectively, is so different than the learning capacity of all other creatures. Human language is a medium for learning together. The richer the language the richer our learning together - the richer our learning together the richer our language.
            ''')
            
            self.sc.authorize("dsfportree", 'nopass', 'all:c')
            op = self.sc.createOp(baseOp['id'], "positive", title="Some humans have conversations that resemble those of monkeys.")
            
            for i in range(1,21):
                self.sc.authAndCreateOp('', op['id'], 'positive')
                
            self.sc.authorize("BarrySpock", 'nopass', 'all:c')
            op = self.sc.createOp(op['id'], "positive", title="Oh snap! you beat me to it.")
            
            for i in range(1,4):
                self.sc.authAndCreateOp('', op['id'], 'positive')
            
            self.sc.authorize("Bear", 'nopass', 'all:c')
            op = self.sc.createOp(baseOp['id'], "neutral", title='If by "ours" you mean "Congress".')
            
            self.sc.authAndCreateOp('', op['id'], 'positive')
            
            self.sc.authorize("G0f0rIt", 'nopass', 'all:c')
            op = self.sc.createOp(baseOp['id'], "positive", title='The learning ability of us humans appears to out-class all other species on Earth, however, so does our un-learning ability.')
            
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()